<?php

/*
 *
 *       _/_/_/                      _/        _/_/_/_/_/
 *    _/          _/_/      _/_/    _/  _/          _/      _/_/      _/_/
 *   _/  _/_/  _/_/_/_/  _/_/_/_/  _/_/          _/      _/    _/  _/    _/
 *  _/    _/  _/        _/        _/  _/      _/        _/    _/  _/    _/
 *   _/_/_/    _/_/_/    _/_/_/  _/    _/  _/_/_/_/_/    _/_/      _/_/
 *
 *
 *  Copyright 2013-2014, Geek Zoo Studio
 *  http://www.ecmobile.cn/license.html
 *
 *  HQ China:
 *    2319 Est.Tower Van Palace
 *    No.2 Guandongdian South Street
 *    Beijing , China
 *
 *  U.S. Office:
 *    One Park Place, Elmira College, NY, 14901, USA
 *
 *  QQ Group:   329673575
 *  BBS:        bbs.ecmobile.cn
 *  Fax:        +86-10-6561-5510
 *  Mail:       info@geek-zoo.com
 */
	include_once(EC_PATH . '/includes/lib_transaction.php');
	include_once(EC_PATH . '/includes/lib_order.php');

	($order_id = _POST('order_id', 0)) || abort('参数错误!');
	$user_id = $_SESSION['user_id'];

	/* 订单详情 */
	$order = get_order_detail($order_id, $user_id);
	//$order_time = _POST('order_time');

	$payment = [];
	if ($order['pay_id'] > 0)
	{
	 	 $payment = payment_info($order['pay_id']);
	}

	// if ($payment['pay_code'] == "upop")
	// {
	// 	include_once(GZ_PATH . '/payment/UPMP/upop_mobile.php');
	// 	$upop = new UPOP_MOBILE();
	// 	$pay_result = $upop->query($order,$payment,$order_time);
	// 	GZ_Api::outPut($pay_result);
	// }

	$return = [
		'order_status' 	=> get_order_status($order['order_status'], $order['shipping_status'], $order['pay_status']),
		'payment' 		=> $payment,
		'order_id'		=> $order['order_id'],
		'order_sn'		=> $order['order_sn'],
		'user_id'		=> $order['user_id'],
		'consignee'		=> $order['consignee'],
		'country'		=> $GLOBALS['db']->getOne("SELECT region_name FROM `region` WHERE region_id={$order['country']}"),
		'province'		=> $GLOBALS['db']->getOne("SELECT region_name FROM `region` WHERE region_id={$order['province']}"),
		'city'			=> $GLOBALS['db']->getOne("SELECT region_name FROM `region` WHERE region_id={$order['city']}"),
		'district'		=> $GLOBALS['db']->getOne("SELECT region_name FROM `region` WHERE region_id={$order['district']}"),
		'address'		=> $order['address'],
		'zipcode'		=> $order['zipcode'],
		'tel'			=> $order['tel'],
		'mobile'		=> $order['mobile'],
		'shipping_id'	=> $order['shipping_id'],
		'shipping_name'	=> $order['shipping_name'],
		'pay_id'		=> $order['pay_id'],
		'pay_name'		=> $order['pay_name'],
		'shipping_fee'	=> $order['shipping_fee'],
		'bonus'			=> $order['bonus'],
		'order_amount'	=> $order['order_amount'],
		'add_time'		=> $order['add_time'],
		'confirm_time'	=> $order['confirm_time'],
		'pay_time'		=> $order['pay_time'],
		'shipping_time'	=> $order['shipping_time'],
		'total_fee'		=> $order['total_fee'],
		'goods_list' 	=> GZ_order_goods($order['order_id'])
	];

	return $return;

	/**
	 * 取得订单商品
	 * @param   int     $order_id   订单id
	 * @return  array   订单商品数组
	 */
	function GZ_order_goods($order_id)
	{
	//    $sql = "SELECT o.*, " .
	//            "o.goods_price * o.goods_number AS subtotal,g.goods_thumb,g.original_img,g.goods_img,o.isEvaluated,o.rec_id " .
	//            "FROM " . $GLOBALS['ecs']->table('order_goods') . " as o LEFT JOIN ".$GLOBALS['ecs']->table('goods') . " AS g ON o.goods_id = g.goods_id" .
	//            " WHERE o.order_id = '$order_id'";
	    $sql = "SELECT o.*, " .
	        "o.goods_price * o.goods_number AS subtotal,g.goods_thumb,g.original_img,g.goods_img,o.rec_id " .
	        "FROM " . $GLOBALS['ecs']->table('order_goods') . " as o LEFT JOIN ".$GLOBALS['ecs']->table('goods') . " AS g ON o.goods_id = g.goods_id" .
	        " WHERE o.order_id = '$order_id'";

	    $res = $GLOBALS['db']->query($sql);

	    while ($row = $GLOBALS['db']->fetchRow($res))
	    {
	        if ($row['extension_code'] == 'package_buy')
	        {
	            $row['package_goods_list'] = get_package_goods($row['goods_id']);
	        }
	        $goods_list[] = $row;

	    }
	    //return $GLOBALS['db']->getAll($sql);
	    return $goods_list;
	}
